package com.itcoon.cloud.framework.i18n.bundle;

import com.itcoon.cloud.framework.ex.errorcode.IErrorCode;
import com.itcoon.cloud.framework.ex.errorcode.IServiceErrorCode;
import com.itcoon.cloud.framework.ex.exception.IException;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Optional;

public class ExBundle<EX extends RuntimeException & IException, E extends IErrorCode> extends Bundle{

    private Class<E> exResponseEnum;

    public Class<E> getExResponseEnum() {
        return exResponseEnum;
    }

    public Class<E> getEx() {
        Type[] genericInterfaces = exResponseEnum.getGenericInterfaces();
        Optional<Type> typeOp = Arrays.stream(genericInterfaces).filter(g->((Class)((ParameterizedType)g).getRawType()).isAssignableFrom(IServiceErrorCode.class)).findAny();
        if(typeOp.isPresent()){
            ParameterizedType type = (ParameterizedType) typeOp.get();
            return (Class<E>) type.getActualTypeArguments()[0];
        }
        return null;
    }

    public void setExResponseEnum(Class<E> exResponseEnum) {
        this.exResponseEnum = exResponseEnum;
    }
}
